package com.zcs.lib.widget;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.greateffect.littlebud.lib.utils.JColors;

public class ScaleAbleImageView extends AppCompatImageView implements View.OnTouchListener {
    private static final float SCALE = 0.95f;

    public ScaleAbleImageView(Context context) {
        this(context, null, 0);
    }

    public ScaleAbleImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScaleAbleImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (isClickable() && isFocusable()) {
            if (isClickable() && isFocusable()) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {// 按下时,图片缩放
                    addScale();
                } else if (event.getAction() == MotionEvent.ACTION_UP
                        || event.getAction() == MotionEvent.ACTION_CANCEL
                        || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
                    removeScale();
                }
            }
            return false;
        } else {
            return super.onTouchEvent(event);
        }
    }

    private void addScale() {
//        setScaleX(SCALE);
//        setScaleY(SCALE);
        setColorFilter(0x20000000);
    }

    private void removeScale() {
//        setScaleX(1f);
//        setScaleY(1f);
        setColorFilter(0x00000000);
    }

    public void autoScale() {
        if (getScaleX() == 1) {// 如果现在已经被用户按了 就不动画了
            ObjectAnimator asx = ObjectAnimator.ofFloat(this, "scaleX", 1f, 0.9f, 1f);
            ObjectAnimator asy = ObjectAnimator.ofFloat(this, "scaleY", 1f, 0.9f, 1f);
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.setDuration(500);
            animatorSet.play(asx).with(asy);
            animatorSet.start();
        }
    }
}